<?php
// $Id$

include_once(APP_LIB_DIR . 'InboxManagerModel.class.php');
require_once(APP_LIB_DIR . 'MessagingModel.class.php');
require_once(APP_LIB_DIR . 'LookupModel.class.php');
include_once(APP_LIB_DIR . 'paging.class.php');
include_once(APP_LIB_DIR . 'JSON.php');

class InboxManagerAction extends Action
{
    function getRequestMethods ()
    {
        return REQ_GET|REQ_POST;
    }

    function isSecure ()
    {
        return true;
    }

    function getPrivilege (&$controller, &$request, &$user)
    {
        return Array('INBOXMANAGEMENT');
    }

    function getDefaultView (&$controller, &$request, &$user)
    {
        return VIEW_INPUT;
    }

    function execute (&$controller, &$request, &$user)
    {
        $auth = $user->getAttribute('auth');
        $GLOBALS['login'] = $auth['username'];
        $GLOBALS['user_id'] = $auth['id'];
        $GLOBALS['user_privilege'] = $auth['privilege'];
        if($request->getParameter('export')){
            $sm     = new InboxManagerModel('InboxManagerAction');
            $search = $request->getParameter('search');
            $list   = $sm->datalist($search);
            $request->setAttribute('list',$list);
        }
        return VIEW_INPUT;
    }

    function getlist($page,$search,$by,$sort)
    {
        $pager  = new Pager; 
        $sm     = new InboxManagerModel('InboxManagerAction');
        $json   = new Services_JSON();
        $search = isset($search['search']) ? $search['search'] : false;
        $search = !empty($search['searchtext']) ? $search : false;
        $count  = $sm->collect(null,null,$search);
        $pager  = $pager->getPagerData($count[0][0],'', $page, "page");
        $offset = ($count[0][0] != 0) ? $pager->offset : 0;
        $limit  = $pager->limit;
        $link   = $pager->showPage;
        $u_id   = $GLOBALS['user_id'];
        $u_prv  = $GLOBALS['user_privilege'];
        $list   = $sm->collect($offset,$limit,$search,$by,$sort,$u_id,$u_prv);
        $data   = array($list,$link);
        return $json->encode($data); 
    }
    function lookup($what)
    {
        $lm     = new LookupModel('InboxManagerAction');
        $json   = new Services_JSON();
          switch($what){
            case 'folders':
		return $json->encode($lm->folders());break;
            default:
		return $json->encode($lm->levels());
          }
    }
    function save($detail, $id,$id1, $action)
    {
                $mm = new MessagingModel('Messaging');
        $sm     = new InboxManagerModel('InboxManagerAction');
        $json   = new Services_JSON();

        if ($action == "get"){
            return $json->encode($sm->detail($id));
        }elseif ($action == "del"){
            //return $json->encode($sm->deactivate($id));
            $detail['modified_by'] = $GLOBALS['login'];
            $detail['active']      = 0;
            $detail['modified_date'] = "NOW()";
            return $json->encode($sm->edit($id,$detail));
        }elseif ($action == "move"){
            //return $json->encode($sm->deactivate($id));
            $detail['folder_id'] = $id1;
            return $json->encode($sm->edit($id,$detail));
        }elseif ($action == "send_msg"){
            //return $json->encode($sm->deactivate($id));
            $detail = $detail['details'];
            $detail['member_id']='';
            $detail['group_id']='';
            $smsc = $detail['smsc'];
            $smsc = ($smsc=='auto') ? false : $smsc;
            //return $json->encode($mm->send_sms($detail['to'], $detail['message'],$smsc,$GLOBALS['login']));
           return $json->encode($mm->send_sms($detail['to'], $detail['message'],$smsc,$GLOBALS['login'],$detail['member_id'],$detail['group_id']));
        }elseif ($id){
            $detail = $this->change_case($detail['details']);
            $detail['modified_by'] = $GLOBALS['login'];
            return $json->encode($sm->edit($id,$detail));
        }else{
            $detail = $this->change_case($detail['details']);
            $detail['auth_id'] = $GLOBALS['user_id'];
            $detail['created_by'] = $GLOBALS['login'];
            $detail['creation_date'] = "NOW()";
            return $json->encode($sm->ins($detail));
        }
    }
    function change_case($array)
    {
        foreach ($array as $key => $val) $d[$key] = strtoupper($val);
        return $d;
    }
}

?>
